<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/8/1
 * Time: 3:32 PM
 */
namespace app\common\services\theme;

use app\common\exceptions\BizException;
use app\common\models\Theme;
use app\common\models\ThemeFile;
use app\common\models\ThemeFileConfigLog;

class ThemeService
{

    public function pageData($themeFileInfo){
        $configs = $themeFileInfo['configs']?json_decode($themeFileInfo['configs'],true):[];
        array_multisort(array_column($themeFileInfo['configs'], 'order'), SORT_ASC, $themeFileInfo['configs']);
        $res['page_info'] = $themeFileInfo['page_info']?json_decode($themeFileInfo['page_info'],true):[];
        $datas = $this->buildPageConfigsData($configs,$themeFileInfo);
        $res['datas'] = $datas;
        unset($res['configs']);//不返回configs，减少数据传输
        return $res;
    }
    /**
     * 获取预览所需的数据
     * @param $pageId
     * @param $type 对应app\common\models\ThemeFileConfigLog里的type常量
     * @return ThemeFile
     * @throws BizException
     */
    public function previewPageData($pageId,$type=0){
        $themeFileConfigInfo = ThemeFileConfigLog::getLastByThemeFile($pageId,$type);
        $res = $themeFileConfigInfo;
        $configs = $themeFileConfigInfo['configs']?json_decode($themeFileConfigInfo['configs'],true):[];
        array_multisort(array_column($res['configs'], 'order'), SORT_ASC, $res['configs']);
        $res['page_info'] = $themeFileConfigInfo['page_info']?json_decode($themeFileConfigInfo['page_info'],true):[];
        if(!$themeFileConfigInfo && $themeFileConfigInfo['id']){
            throw new BizException('未找到数据');
        }
        $themeFileInfo = ThemeFile::site()->select()
            ->where('id',$pageId)
            ->first();
        $datas = $this->buildPageConfigsData($configs,$themeFileInfo);
        $res['datas'] = $datas;
        unset($res['configs']);//不返回configs，减少数据传输
        return $res;
    }

    /**
     * 构建页面显示用的datas
     * @param $configs
     * @param $themeFileInfo
     * @return array
     * @throws BizException
     */
    public function buildPageConfigsData($configs,$themeFileInfo){
        $pageConfigs = [];
        if($themeFileInfo['layout']){
            //存在布局文件，读取布局文件的配置信息
            $layoutInfo = ThemeFile::site()->select()
                ->where('theme_id',$themeFileInfo['theme_id'])
                ->where('file_name',$themeFileInfo['layout'])
                ->first();
            if($layoutInfo['id']){
                //合并配置中block信息
                $layoutConfigs = $layoutInfo['configs']?json_decode($layoutInfo['configs'],true):[];
                foreach ($layoutConfigs as $k=>$v){
                    if($k!='slot'){
                        //todo 是否考虑多个slot
                        $pageConfigs[$k] = $v;
                    }else{
                        //slot后面插入页面的blocks
                        foreach ($configs as $key=>$val){
                            $pageConfigs[$key] = $val;
                        }
                    }
                }
                unset($pageConfigs['slot']);
            }
        }else{
            $pageConfigs = $configs;
        }
        $datas = [];//预览和发布后的页面中的数据放到$res['datas']，减少层级
        foreach ($pageConfigs as $k=>$v){
            foreach ($pageConfigs[$k]['configs'] as $key=>$val){
                if($val['dataType']){
                    $datas[$k][$key] = ConfigDataFactory::create($val['dataType'])->getData($val['value']);
                    //$res['configs'][$k]['configs'][$key]['datas'] = ConfigDataFactory::create($val['dataType'])->getData($val['value']);
                }else{
                    $datas[$k][$key] = $val['value'];
                    //$res['configs'][$k]['configs'][$key]['datas'] = $val['value'];
                }
            }
        }
        return $datas;
    }
    /**
     * 获取默认模版中对应type的数据
     * @param $type
     * @return ThemeFile
     */
    public function getDefaultThemeInfo($type,$pageType){
        $excludeType = $this->getExcludeType($type);
        $defaultTheme = Theme::site()->select()
            ->with(['themeFiles' => function ($query) use($pageType){
                return $query->select()
                    ->where('type',$pageType);
            }])
            ->where('status',Theme::STATUS_NORMAL)
            ->where('is_default',Theme::STATUS_YES)
            ->where('type','!=',$excludeType)
            ->first();
        return $defaultTheme;
    }

    public function getExcludeType($type){
        $excludeType = Theme::TYPE_PC;
        if($type==Theme::TYPE_PC){
            $excludeType = Theme::TYPE_H5;
        }
        return $excludeType;
    }
}